import { t } from 'i18next'
import dayjs from '@/plugins/dayjs'
import { resultMeeting } from './interface'
import { MeetingInfo } from '@/api/myMeeting/interface'

function getDrawGrid(time: dayjs.Dayjs) {
	const hourGrids = time.hour() * 4
	const minuteGrids = Math.round(time.minute() / 15)
	return hourGrids + minuteGrids
}

export function transMeetingList(m: MeetingInfo[]) {
	m.sort((a, b) => (dayjs(a.start_time).isAfter(dayjs(b.start_time)) ? 1 : -1))
	let result: resultMeeting[] = []
	//存在跨天会议，需要重整数据方便渲染;  开始时间和结束时间为了界面显示渲染，都会模糊处理，以15分钟为间隔并包含该时间在内
	for (const meetingInfo of m) {
		let startDay = dayjs(meetingInfo.start_time, 'YYYY:MM:DD')
		let endTime = meetingInfo.end_time ?? `${startDay.format('YYYY-MM-DD')} 23:59:59`
		let endDay = dayjs(endTime, 'YYYY:MM:DD')
		let _startTime = dayjs(meetingInfo.start_time)
		let _endTime = dayjs(endTime)
		let startGrid = getDrawGrid(_startTime)
		let endGrid = getDrawGrid(_endTime)
		if (endGrid - startGrid < 1) {
			endGrid = startGrid + 1
		}
		if (_startTime.format('YYYY:MM:DD') === _endTime.format('YYYY:MM:DD')) {
			result.push({
				meeting: meetingInfo,
				day: startDay,
				startTime: _startTime,
				endTime: _endTime,
				startGrid,
				endGrid,
				col: 1,
				_total: { num: 1 },
				_weekDay: _startTime.day(),
			})
		} else {
			//结束日期和初始日期 相差几天 --- 垮天显示
			let diffDays = endDay.diff(startDay, 'day')
			let lastRender = true
			//特殊情况，结束时间是开始时间的第n天的零点，例如 2023-06-29T20:00:00+08:00 和 2023-06-30T00:00:00+08:00
			if (_endTime.format('HH:mm:ss') === '00:00:00') {
				diffDays = diffDays - 1
				lastRender = false
			}
			result.push({
				meeting: meetingInfo,
				day: startDay,
				startTime: _startTime,
				endTime: dayjs(`${startDay.format('YYYY-MM-DD')} 23:59:59`),
				startGrid,
				endGrid: 96,
				col: 1,
				_total: { num: 1 },
				_weekDay: _startTime.day(),
			})
			for (let i = 1; i < diffDays; i++) {
				let d = dayjs(startDay).add(i, 'day')
				result.push({
					meeting: meetingInfo,
					day: d,
					startTime: dayjs(`${d.format('YYYY-MM-DD')} 00:00:00`),
					endTime: dayjs(`${d.format('YYYY-MM-DD')} 23:59:59`),
					startGrid: 0,
					endGrid: 96,
					col: 1,
					_total: { num: 1 },
					_weekDay: dayjs(`${d.format('YYYY-MM-DD')} 00:00:00`).day(),
				})
			}
			if (lastRender) {
				result.push({
					meeting: meetingInfo,
					day: endDay,
					startTime: dayjs(`${endDay.format('YYYY-MM-DD')} 00:00:00`),
					endTime: _endTime,
					startGrid: 0,
					endGrid: getDrawGrid(_endTime),
					col: 1,
					_total: { num: 1 },
					_weekDay: dayjs(`${endDay.format('YYYY-MM-DD')} 00:00:00`).day(),
				})
			}
			// //不跨天显示
			// result.push({
			// 	meeting: meetingInfo,
			// 	day: startDay,
			// 	startTime: _startTime,
			// 	endTime: dayjs(`${startDay.format('YYYY-MM-DD')} 23:59:59`),
			// 	startGrid,
			// 	endGrid: 96,
			// 	col: 1,
			// 	_total: { num: 1 },
			// })
		}
	}
	let _result: resultMeeting[] = []
	for (let i = 0; i < result.length; i++) {
		let item = result[i]
		if (item._flag) continue
		_result.push(item)
		item._flag = 1
		//针对时间相同的数据只取前500条
		let sameList = result
			.filter((x) => {
				if (
					x._weekDay === item._weekDay &&
					x.startGrid === item.startGrid &&
					x.endGrid === item.endGrid &&
					!x._flag
				) {
					x._flag = 1
					return true
				}
				return false
			})
			.slice(0, 500)
		_result = _result.concat(sameList)
	}

	//计算偏移量
	_result = _result.sort((a, b) => {
		let aGap = a.endGrid - a.startGrid
		let bGap = b.endGrid - b.startGrid
		return bGap - aGap
	})

	for (let i = 0; i < _result.length; i++) {
		let item = _result[i]
		let itemContainerList = _result
			.slice(0, i)
			.filter(
				(x) =>
					x._weekDay === item._weekDay &&
					((x.startGrid < item.startGrid && x.endGrid > item.startGrid) ||
						(x.startGrid === item.startGrid && x.meeting.meeting_moid !== item.meeting.meeting_moid))
			)
		if (itemContainerList.length > 0) {
			item.col = itemContainerList.length + 1
			//父级一定是最后一位，采用引用赋值，父子的分割总块数相同。
			let parent = itemContainerList[0]
			parent._total.num = Math.max(parent._total.num, item.col)
			item._total = parent._total
		}
	}
	return _result.map((x) => ({
		meeting: x.meeting,
		day: x.day,
		startGrid: x.startGrid,
		endGrid: x.endGrid,
		col: x.col,
		totalSpan: x._total.num,
	}))
}
export function percentOfDay(time: dayjs.Dayjs) {
	return ((time.hour() + time.minute() / 60) / 24) * 100 + '%'
}
export const TIME = new Array(24).fill(0).map((_, i) => (i > 9 ? `${i}:00` : `0${i}:00`))
export const weekList = [t('日'), t('一'), t('二'), t('三'), t('四'), t('五'), t('六')]
/** 获取手机和分机号 */
export function getPhone(mobile?: string, telephone?: string) {
	let str = ''
	if (mobile && telephone) {
		str += '(' + mobile + '-' + telephone + ')'
	} else if (mobile) {
		str += '(' + mobile + ')'
	} else if (telephone) {
		str += '(' + telephone + ')'
	}
	return str
}
